<script lang="ts" setup>
import { useVbenModal } from '@vben/common-ui';

import { message, TabPane, Tabs } from 'ant-design-vue';

import { useVbenForm } from '#/adapter/form';
import { postMerchants, putMerchants } from '#/api/plugin/pay';

import {
  alipayScheam,
  mergeValues,
  unipayScheam,
  wechatScheam,
} from './scheam';

const activeKey = ref(1);
const disabled = ref(true);
const [BasicForm, BasicFormApi] = useVbenForm({
  handleSubmit: onSubmit,
  resetButtonOptions: {
    show: false,
  },
  schema: [
    {
      component: 'Input',
      defaultValue: '',
      dependencies: {
        show: false,
        triggerFields: ['id'],
      },
      fieldName: 'id',
      label: 'ID',
    },
    {
      component: 'Input',
      defaultValue: '',
      dependencies: {
        trigger: (values) => {
          mergeValues.value.name = values.name;
          disabled.value = !values.name;
        },
        // 随意一个字段改变时，都会触发
        triggerFields: ['name'],
      },
      componentProps: {
        placeholder: '请输入',
      },
      fieldName: 'name',
      label: '商户名称',
      rules: 'required',
    },
  ],
});

const [WechatForm, WechatFormApi] = useVbenForm({
  handleSubmit: onSubmit,
  resetButtonOptions: {
    show: false,
  },
  schema: wechatScheam,
});

const [AlipayForm, AlipayFormApi] = useVbenForm({
  handleSubmit: onSubmit,
  resetButtonOptions: {
    show: false,
  },
  schema: alipayScheam,
});

const [UnipayForm, UnipayFormFormApi] = useVbenForm({
  handleSubmit: onSubmit,
  resetButtonOptions: {
    show: false,
  },
  schema: unipayScheam,
});

const [Modal, formModalApi] = useVbenModal({
  fullscreenButton: false,
  footer: false,
  onBeforeClose() {
    activeKey.value = 1;
    disabled.value = true;
    mergeValues.value.id = '';
  },
  onOpenChange(isOpen: boolean) {
    if (isOpen) {
      const values: any = formModalApi.getData();
      if (values?.id) {
        BasicFormApi.setValues({ id: values.id, name: values.name });
        WechatFormApi.setValues(values.config.wechat.default);
        AlipayFormApi.setValues(values.config.alipay.default);
        UnipayFormFormApi.setValues(values.config.unipay.default);
        mergeValues.value.config = values.config;
        mergeValues.value.id = values.id;
        mergeValues.value.name = values.name;
      }
    }
  },
});

async function onSubmit() {
  function showMsg(msg: string) {
    message.success(msg);
    formModalApi.setData({ isUpdata: true });
    formModalApi.close();
  }
  const values: any = mergeValues.value;

  if (values.id) {
    const { msg } = await putMerchants(values);
    showMsg(msg);
  } else {
    delete values.id;
    const { msg } = await postMerchants(values);
    showMsg(msg);
  }
}
</script>
<template>
  <Modal class="w-3/5">
    <Tabs v-model:active-key="activeKey">
      <TabPane :key="1" tab="基本信息">
        <BasicForm />
      </TabPane>
      <TabPane :key="2" :disabled="disabled" tab="微信支付">
        <WechatForm />
      </TabPane>
      <TabPane :key="3" :disabled="disabled" tab="支付宝支付">
        <AlipayForm />
      </TabPane>
      <TabPane :key="4" :disabled="disabled" tab="银联支付">
        <UnipayForm />
      </TabPane>
    </Tabs>
  </Modal>
</template>

<style lang="scss" scoped></style>
